package net.towee.server.bittorrent;

import javax.servlet.Servlet;

import net.towee.model.bittorrent.Peer;
import net.towee.model.bittorrent.TorrentData;

/**
 * This interface define bittorrent announce service
 * @author c58
 */
public interface AnnounceService extends Servlet {
	
	/**
	 * Errors what may be while processing AnnounceRequest
	 * @author c58
	 */
	public enum AnnounceError {
		UNKNOWN_TORRENT,
		MISSING_PEERID,
		MISSING_INFOHASH,
		MISSING_PORT, 
		MISSING_UL,
		MISSING_DL,
        MISSING_EVENT,
        INTERNAL_ERROR,
        MALFORMED_REQUEST
	}
	
	/**
	 * Announce event
	 * @author c58
	 */
	public enum AnnounceEvent {
		STARTED, STOPPED, COMPLETED, REGULAR
	}
	
	/**
	 * Consists of data received from peer
	 * @author c58
	 */
	public interface AnnounceRequest {
		String getInfoHash();
		
		String getPeerId();
		
		String getIp();
		
		int getPort();
		
		long getUploaded();
		
		long getDownloaded();
		
		AnnounceEvent getEvent();
		
		String getAccountId();
	}
	
	/**
	 * This method process announce request and write response.
	 * 
	 * Implementation should validate request data and try to get
	 * torrent with given InfoHash. 
	 * 
	 * @param request
	 * @param out
	 */
	void processAnnounceRequest(AnnounceRequest request, Appendable out);
	
	/**
	 * Create a peer from announce request information
	 * @param request
	 * @return
	 */
	Peer createPeer(AnnounceRequest request);
	
	/**
	 * Write an announce processing error 
	 * @param error
	 * @param out
	 */
	void writeError(AnnounceError error, Appendable out);

	/**
	 * Write bencoded response with peers and interval
	 * @param torrent
	 * @param out
	 */
	void writePeersAndInterval(TorrentData torrent, Appendable out);
}
